In [1]:
using ViscousFlow
In [2]:
using Plots
pyplot()
clibrary(:colorbrewer)
default(grid = false)
In [3]:
using Compat
using Compat: range
First create a grid and a Laplacian operator on it
In [4]:
nx = 130; ny = 130;
Lx = 2.0;
dx = Lx/(nx-2);
w = Nodes(Dual,(nx,ny));
In [5]:
L = plan_laplacian(size(w),with_inverse=true)
Out[5]:
Now create a shape, with discrete points and associated regularization and interpolation operator
In [6]:
n = 128;
θ = range(0,stop=2π,length=n+1);
R = 0.5;
xb = 1.0 .+ R*cos.(θ)
yb = 1.0 .+ R*sin.(θ)
ds = (2π/n)*R;
X = VectorData(xb[1:n],yb[1:n]);
f = ScalarData(X);
In [7]:
E = Regularize(X,dx;ddftype=Fields.Roma,issymmetric=true)
Hmat,Emat = RegularizationMatrix(E,f,w);
In [8]:
PS = SaddleSystem((w,f),(x->L\x,Hmat,Emat),issymmetric=true,isposdef=true)
Out[8]:
In [9]:
ψb = ScalarData(X);
w = Nodes(Dual,(nx,ny));
ψb .= -(xb[1:n] .- 1);
f .= ones(Float64,n)*ds;
ψ = Nodes(Dual,w);
@time ψ,f = PS\(w,ψb)
xg,yg = coordinates(ψ,dx=dx)
plot(xg,yg,ψ)
plot!(xb,yb,fillcolor=:black,fillrange=0,fillalpha=0.25,linecolor=:black)
Out[9]:
In [10]:
using LinearAlgebra
In [11]:
fex = -2*cos.(θ[1:n]);
errinf = LinearAlgebra.norm(f./ds-fex,Inf)
Out[11]:
In [12]:
plot(f./ds,label="Numerical")
plot!(fex,label="Exact")
Out[12]:
In [13]:
PSstore = SaddleSystem((w,f),(x->L\x,Hmat,Emat),issymmetric=true,isposdef=true,store=true)
Out[13]:
This way is significantly faster
In [14]:
ψb = ScalarData(X);
w = Nodes(Dual,(nx,ny));
ψb .= -(xb[1:n] .- 1);
f .= ones(Float64,n)*ds;
ψ = Nodes(Dual,w);
@time ψ,f = PSstore\(w,ψb)
xg,yg = coordinates(ψ,dx=dx)
plot(xg,yg,ψ)
plot!(xb,yb,fillcolor=:black,fillrange=0,fillalpha=0.25,linecolor=:black)
Out[14]:
But there is significantly more noise in the force
In [15]:
fex = -2*cos.(θ[1:n]);
errinf = norm(f./ds-fex,Inf)
Out[15]:
In [16]:
plot(f)
Out[16]:
So let's filter it
In [17]:
Ẽ = Regularize(X,dx;weights=ds,filter=true)
H̃mat = RegularizationMatrix(Ẽ,f,w);
Ẽmat = InterpolationMatrix(Ẽ,w,f);
In [18]:
f̃ = ScalarData(X);
In [19]:
P(f) = Ẽmat*(H̃mat*f)
PScond = SaddleSystem((w,f̃),(x->L\x,Hmat,Emat),issymmetric=true,isposdef=true,conditioner=P,store=true)
Out[19]:
In [20]:
ψb = ScalarData(X);
w = Nodes(Dual,(nx,ny));
ψb .= -(xb[1:n] .- 1);
ψ = Nodes(Dual,w);
@time ψ,f̃ = PScond\(w,ψb)
xg,yg = coordinates(ψ,dx=dx)
plot(xg,yg,ψ)
plot!(xb,yb,fillcolor=:black,fillrange=0,fillalpha=0.25,linecolor=:black)
Out[20]:
In [21]:
fex = -2*cos.(θ[1:n]);
errinf = norm(f̃./ds-fex,Inf)
Out[21]:
In [22]:
plot(f./ds,label="Numerical")
plot!(f̃./ds,label="Numerical w/filtering")
plot!(fex,label="Exact")
Out[22]:
In [23]:
Δt = 1.0
HS = SaddleSystem((w,f),(plan_intfact(Δt,w),Hmat,Emat),issymmetric=true,isposdef=true)
Out[23]:
In [24]:
ψb = ScalarData(X);
w = Nodes(Dual,(nx,ny));
ψb .= -(xb[1:n] .- 1).*(yb[1:n] .- 1);
f .= ones(Float64,n)*ds;
ψ = Nodes(Dual,w);
@time ψ,f = HS\(w,ψb)
Out[24]:
In [25]:
xg,yg = coordinates(ψ,dx=dx)
plot(xg,yg,ψ)
plot!(xb,yb,fillcolor=:black,fillrange=0,fillalpha=0.25,linecolor=:black)
Out[25]:
In [26]:
u = ones(Float64,2)
f = Vector{Float64}()
A⁻¹(u::Vector{Float64}) = u
B₁ᵀ(f::Vector{Float64}) = zeros(Float64,2)
B₂(u::Vector{Float64}) = Vector{Float64}()
sys = SaddleSystem((u,f),(A⁻¹,B₁ᵀ,B₂))
Out[26]:
In [27]:
sys\(u,f)
Out[27]:
In [28]:
ψb = ScalarData(X);
f = ScalarData(X);
w = Nodes(Dual,(nx,ny));
ψb .= -(xb[1:n] .- 1);
f .= ones(Float64,n)*ds;
ψ = Nodes(Dual,w);
In [29]:
u = ones(Float64,2)
f = Vector{Float64}()
A⁻¹(u::Vector{Float64}) = u
B₁ᵀ(f::Vector{Float64}) = zeros(Float64,2)
B₂(u::Vector{Float64}) = Vector{Float64}()
sys = SaddleSystem((u,f),(A⁻¹,B₁ᵀ,B₂))
Out[29]:
In [30]:
sysys = (PS,sys)
rhs = ((w,ψb),(u,f))
(ψ,f), (u,_) = sysys\rhs
Out[30]:
In [ ]: